﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TCPClient.UI.LogicForUserControls;

namespace TCPClient.UI
{
    public enum eNotificationType
    {
        NotifyForNewWorkOrder,
        NotifyForMissingRequests
    }

    public partial class PublishWorkOrderForm : Form
    {
        private eNotificationType m_NotifType;
        private string m_FinalMessage = "";
        private const string k_TitleForNotifAll = "Notify Workers With Work Order: ";
        private const string k_TitleForNotifNotSentRequests = "Notify Workers That Haven't Sent Working Request: ";
        private const string k_TitleForNotifAllResult = "Notification Email with the new work order was successfully sent to all workers";
        private const string k_TitleForNotifNotSentRequestsResult = "Notification Email to workers that haven't sent requests was successfully sent";

        public PublishWorkOrderForm(eNotificationType notifType)
        {
            InitializeComponent();
            m_NotifType = notifType;
            setTitle();
        }

        private void setTitle()
        {
            labelPublish.Text = m_NotifType == eNotificationType.NotifyForNewWorkOrder
                                    ? k_TitleForNotifAll
                                    : k_TitleForNotifNotSentRequests;
            string date = m_NotifType == eNotificationType.NotifyForMissingRequests
                              ? string.Format("\n                           {0} - {1}",
                                              ScMainUserControlLogicManager.Instance.StartTimeOfCurrentWeek.AddDays(7).
                                                  ToShortDateString(),
                                              ScMainUserControlLogicManager.Instance.EndTimeOfCurrentWeek.AddDays(7).
                                                  ToShortDateString())
                              : string.Format("{0} - {1}",
                                              ScMainUserControlLogicManager.Instance.StartTimeOfCurrentWeek.AddDays(7).
                                                  ToShortDateString(),
                                              ScMainUserControlLogicManager.Instance.EndTimeOfCurrentWeek.AddDays(7).
                                                  ToShortDateString());
            labelPublish.Text += date;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void sendMails()
        {
            switch (m_NotifType)
            {
                case eNotificationType.NotifyForNewWorkOrder:
                    ScMainUserControlLogicManager.Instance.NotifyAllWorkersForNewShiftCreation(textBox1.Text);
                    m_FinalMessage = k_TitleForNotifAllResult;
                    break;
                default:
                    ScMainUserControlLogicManager.Instance.NotifyWorkersThatnNotSendRequests(textBox1.Text);
                    m_FinalMessage = k_TitleForNotifNotSentRequestsResult;
                    break;
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            progressBarSendMails.Visible = true;
            progressBarSendMails.Maximum = 400;
            disableAll();
            timer1.Start();
            Thread thread = new Thread(sendMails);
            thread.Start();
        }

        private void disableAll()
        {
            buttonCancel.Enabled = false;
            buttonSend.Enabled = false;
            textBox1.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBarSendMails.Increment(1);
            if (progressBarSendMails.Value == 400)
            {
                finishedSendMails();
            }
        }

        private void finishedSendMails()
        {
            timer1.Stop();
            progressBarSendMails.Value = 0;
            MessageBox.Show(m_FinalMessage, @"Notify Workers", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }
    }
}
